﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using SdlDotNet.Graphics;
using SdlDotNet.Graphics.Primitives;
using SdlDotNet.Core;
using SdlDotNet.Input;

namespace Mech_Force
{
    public class Program
    {
        #region Globals
        private Surface m_VideoScreen;
        private Surface FontSurface;
        private Point MouseCursorPos = new Point();
        private bool MouseButtonDownP = false;
        private bool MouseButtonDownS = false;
        private int window_active = 0;

        private static string default_font = "bt_oldstyle.ttf";
        private static string fontdirectory = "Fonts";
        private static string FilePath = Path.Combine("..", "..");

        private SdlDotNet.Graphics.Font fontLarge = new SdlDotNet.Graphics.Font(Path.Combine(FilePath, Path.Combine(fontdirectory, default_font)), 20);
        private SdlDotNet.Graphics.Font fontMedium = new SdlDotNet.Graphics.Font(Path.Combine(FilePath, Path.Combine(fontdirectory, default_font)), 12);

        //(@".\\Fonts\bt_oldstyle.ttf", 20);
        //public static SdlDotNet.Graphics.Font fontMedium = new SdlDotNet.Graphics.Font(@".\\Fonts\bt_oldstyle.ttf", 12);

        private enum Screens : int
        {
            Default = 0,
            Screen1 = 1,
            Screen2 = 2,
            Screen3 = 3
        }

        #endregion

        public static void Main(string[] args)
        {
            Mech_Force.Program prog = new Program();
            prog.Go();
        }

        public void Go()
        {
            m_VideoScreen = Video.SetVideoMode(640, 480, 32, false, false, false, true);

            m_VideoScreen.TransparentColor = System.Drawing.Color.Magenta;
            m_VideoScreen.Transparent = true;

            IntroScreen();
            Events.MouseMotion += new EventHandler<MouseMotionEventArgs>(Events_MouseMotion);
            Events.MouseButtonDown += new EventHandler<MouseButtonEventArgs>(ApplicationMouseButtonEventHandler);
            Events.MouseButtonUp += new EventHandler<MouseButtonEventArgs>(ApplicationMouseButtonEventHandler);
            Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(KeyboardEventHandler);
            Events.KeyboardUp += new EventHandler<KeyboardEventArgs>(KeyboardEventHandler);
            Events.Tick += new EventHandler<TickEventArgs>(ApplicationTickEventHandler);
            Events.Quit += new EventHandler<QuitEventArgs>(ApplicationQuitEventHandler);
            Events.Run();
        }

        private void IntroScreen()
        {
            PutText(FontSurface, m_VideoScreen, fontLarge, "Battle Mech", 200, 60, Color.Green);
            PutText(FontSurface, m_VideoScreen, fontMedium, "Enter Combat", 250, 180, Color.White);
            PutText(FontSurface, m_VideoScreen, fontMedium, "Factory", 280, 200, Color.White);
            PutText(FontSurface, m_VideoScreen, fontMedium, "Guild", 295, 220, Color.White);
            PutText(FontSurface, m_VideoScreen, fontMedium, "Exit Game", 275, 240, Color.White);

        }

        #region EventHandlers
        private void ApplicationMouseButtonEventHandler(object sender, MouseButtonEventArgs args)
        {
            if (args.Button == MouseButton.PrimaryButton)
                MouseButtonDownP = args.ButtonPressed;
            else if (args.Button == MouseButton.SecondaryButton)
                MouseButtonDownS = args.ButtonPressed;
        }

        private void Events_MouseMotion(object sender, MouseMotionEventArgs args)
        {
            MouseCursorPos = args.Position;
        }

        private void KeyboardEventHandler(object sender, KeyboardEventArgs args)
        {
            switch (args.Key)
            {
                case Key.Escape:
                    Events.QuitApplication();
                    break;
            }
        }
        private void ApplicationTickEventHandler(object sender, TickEventArgs args)
        {
            m_VideoScreen.Fill(Color.Black);
            IntroScreen();
            if (MouseCursorPos.X > 250 && MouseCursorPos.X < 390 && MouseCursorPos.Y > 180 && MouseCursorPos.Y < 190)
            {
                FontSurface = fontMedium.Render("Enter Combat", Color.Red);
                m_VideoScreen.Blit(FontSurface, new Point(250, 180));
            }
            if (MouseButtonDownP && MouseCursorPos.X > 250 && MouseCursorPos.X < 390 && MouseCursorPos.Y > 180 && MouseCursorPos.Y < 190)
            {
                window_active = 1;
            }

            if (MouseCursorPos.X > 280 && MouseCursorPos.X < 360 && MouseCursorPos.Y > 200 && MouseCursorPos.Y < 210)
            {
                FontSurface = fontMedium.Render("Factory", Color.Red);
                m_VideoScreen.Blit(FontSurface, new Point(280, 200));
            }
            if (MouseButtonDownP && MouseCursorPos.X > 280 && MouseCursorPos.X < 360 && MouseCursorPos.Y > 200 && MouseCursorPos.Y < 210)
            {
                window_active = 2;
            }

            if (MouseCursorPos.X > 295 && MouseCursorPos.X < 345 && MouseCursorPos.Y > 220 && MouseCursorPos.Y < 230)
            {
                FontSurface = fontMedium.Render("Guild", Color.Red);
                m_VideoScreen.Blit(FontSurface, new Point(295, 220));
            }
            if (MouseButtonDownP && MouseCursorPos.X > 295 && MouseCursorPos.X < 345 && MouseCursorPos.Y > 220 && MouseCursorPos.Y < 230)
            {
                window_active = 3;
            }

            if (MouseCursorPos.X > 275 && MouseCursorPos.X < 375 && MouseCursorPos.Y > 240 && MouseCursorPos.Y < 250)
            {
                FontSurface = fontMedium.Render("Exit Game", Color.Red);
                m_VideoScreen.Blit(FontSurface, new Point(275, 240));
            }
            if (MouseButtonDownP && MouseCursorPos.X > 275 && MouseCursorPos.X < 365 && MouseCursorPos.Y > 240 && MouseCursorPos.Y < 250)
            {
                Events.QuitApplication();
            }

            LoadImages();
            m_VideoScreen.Update();
        }

        private void ApplicationQuitEventHandler(object sender, QuitEventArgs args)
        {
            Events.QuitApplication();
        }

        #endregion

        #region Functions
        /// <summary>
        /// Function to put text on the screen
        /// </summary>
        /// <param name="pSurface">A pre-defined Surface for the font.</param>
        /// <param name="pVideoSurface">A pre-defined Video surface to draw the font to.</param>
        /// <param name="pFont">The font object</param>
        /// <param name="pText">The text to draw</param>
        /// <param name="sx">the x coordinate of the text</param>
        /// <param name="sy">the y coordinate of the text</param>
        /// <param name="pColor">Color of the text</param>
        public void PutText(Surface pSurface, Surface pVideoSurface, SdlDotNet.Graphics.Font pFont, string pText, int sx, int sy, Color pColor)
        {
            pSurface = pFont.Render(pText, pColor);
            pVideoSurface.Blit(pSurface, new Point(sx, sy));
        }

        /// <summary>
        /// The PutText function but allows you to specify an Int instead of a string.
        /// </summary>
        /// <param name="pSurface">A pre-defined Surface for the font.</param>
        /// <param name="pVideoSurface">A pre-defined Video surface to draw the font to.</param>
        /// <param name="pFont">The font object</param>
        /// <param name="pTextInt">A valid Int (often a variable)</param>
        /// <param name="sx">the x coordinate of the text</param>
        /// <param name="sy">the y coordinate of the text</param>
        /// <param name="pColor">Color of the text</param>
        public void PutInt(Surface pSurface, Surface pVideoSurface, SdlDotNet.Graphics.Font pFont, int pTextInt, int sx, int sy, Color pColor)
        {
            pSurface = pFont.Render(Convert.ToString(pTextInt), pColor);
            pVideoSurface.Blit(pSurface, new Point(sx, sy));
        }
        private void LoadImages()
        {
            switch (window_active)
            {
                case 1:
                    {
                        m_VideoScreen.Fill(Color.Black);
                        PutText(FontSurface, m_VideoScreen, fontMedium, "Combat", 120, 60, Color.Blue);
                        if (MouseButtonDownS)
                            window_active = 0;
                    }
                    break;
                case 2:
                    {
                        m_VideoScreen.Fill(Color.Black);
                        PutText(FontSurface, m_VideoScreen, fontMedium, "Factory", 100, 100, Color.Blue);
                        if (MouseButtonDownS)
                            window_active = 0;
                    }
                    break;
                case 3:
                    {
                        m_VideoScreen.Fill(Color.Black);
                        PutText(FontSurface, m_VideoScreen, fontMedium, "Guild", 100, 100, Color.Blue);
                        if (MouseButtonDownS)
                            window_active = 0;
                    }
                    break;
                default:
                    {
                        window_active = 0;
                    }
                    break;
            }
        }
        #endregion
    }
}
